/**
 * 
 */
package de.midnightdance.admin.user;
/**
 * 
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.IndexedPropertyChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import de.cbf.util.ui.editentities.controller.ControllerEditEntity;
import de.cbf.util.ui.editentities.interfaces.ICompositeEditEntity;
import de.cbf.util.ui.editentities.interfaces.IEditableEntity;
import de.midnightdance.admin.ws.entities.RequiemUser;

/**
 * @author Stefan Alter
 *
 */
public class PresenterEditUser extends ControllerEditEntity {
	private PCLUser pclUser;	
	
	private ListenerModifiedName		listenerModifiedName		;
	private ListenerModifiedPass		listenerModifiedPass		;
	private ListenerModifiedPermissions	listenerModifiedPermissions	;
	/**
	 * 
	 */
	public PresenterEditUser() {
		super();
		pclUser	= new PCLUser();
		//
		listenerModifiedName		= new ListenerModifiedName();
		listenerModifiedPass		= new ListenerModifiedPass();	
		listenerModifiedPermissions = new ListenerModifiedPermissions();
	}
	
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntity#checkEntity(de.cbf.util.ui.editentities.interfaces.IEditableEntity)
	 */
	@Override
	protected boolean checkEntity(IEditableEntity value)
			throws IllegalArgumentException {
		return (value instanceof RequiemUser);
	}
	
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntity#checkView(de.cbf.util.ui.editentities.interfaces.ICompositeEditEntity)
	 */
	@Override
	protected boolean checkView(ICompositeEditEntity value)
			throws IllegalArgumentException {
		return (value instanceof CompositeEditUser);		
	}
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntity#registerViewListenersAndInit(de.cbf.util.ui.editentities.interfaces.ICompositeEditEntity)
	 */
	@Override
	protected boolean registerViewListenersAndInit(ICompositeEditEntity value) {
		if (value instanceof CompositeEditUser) {
			CompositeEditUser composite = (CompositeEditUser) value;
			composite.registerModifiedNameListener			(listenerModifiedName);
			composite.registerModifiedPassListener			(listenerModifiedPass);	
			composite.registerModifiedPermissionsListener	(listenerModifiedPermissions);
			return true;
		}
		return false;
	}
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntity#deRegisterViewListeners(de.cbf.util.ui.editentities.interfaces.ICompositeEditEntity)
	 */
	@Override
	protected boolean deRegisterViewListeners(ICompositeEditEntity value) {
		if (value instanceof CompositeEditUser) {
			CompositeEditUser composite = (CompositeEditUser) value;
			composite.deRegisterModifiedNameListener		(listenerModifiedName);
			composite.deRegisterModifiedPassListener		(listenerModifiedPass);	
			composite.deRegisterModifiedPermissionsListener	(listenerModifiedPermissions);
			return true;
		}
		return false;
	}
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntity#getPCLforEntity()
	 */
	@Override
	protected PropertyChangeListener getPCLforEntity() {
		return pclUser;
	}
	
	public void modifiedName() {
		if ((entity instanceof RequiemUser)&& (view instanceof CompositeEditUser)) {
			RequiemUser user = (RequiemUser) entity;
			CompositeEditUser composite = (CompositeEditUser)view;
			//
			user.setName(composite.getUserName());
			//
			testUnsavedChanges();
		}	
	}
	public void modifiedNotizen() {
		if ((entity instanceof RequiemUser)&& (view instanceof CompositeEditUser)) {
			RequiemUser user = (RequiemUser) entity;
			CompositeEditUser composite = (CompositeEditUser)view;
			//
			user.setPass(composite.getPass());
			//
			testUnsavedChanges();
		}		
	}	
	public void modifiedPermissions() {
		if ((entity instanceof RequiemUser)&& (view instanceof CompositeEditUser)) {
			RequiemUser user = (RequiemUser) entity;
			CompositeEditUser composite = (CompositeEditUser)view;
			//
			user.setPermissions(composite.getPermissions());
			//
			testUnsavedChanges();
		}	
	}
	
	public void updateName() {
		if ((entity instanceof RequiemUser)&& (view instanceof CompositeEditUser)) {
			RequiemUser user = (RequiemUser) entity;
			CompositeEditUser composite = (CompositeEditUser)view;
			//
			composite.setUserName(user.getName());			
		}	
	}
	public void updatePass() {
		if ((entity instanceof RequiemUser)&& (view instanceof CompositeEditUser)) {
			RequiemUser user = (RequiemUser) entity;
			CompositeEditUser composite = (CompositeEditUser)view;
			//
			composite.setPass(user.getPass());
		}	
	}
	
	public void updatePermissions() {
		if ((entity instanceof RequiemUser)&& (view instanceof CompositeEditUser)) {
			RequiemUser user = (RequiemUser) entity;
			CompositeEditUser composite = (CompositeEditUser)view;
			//
			composite.setPersmissions(user.iteratorPermissions());
		}	
	}	
	
	private class PCLUser implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent evt) {			
			if (evt instanceof IndexedPropertyChangeEvent) {
				IndexedPropertyChangeEvent idxEvent = (IndexedPropertyChangeEvent) evt;
				switch (idxEvent.getIndex()) {					
					case RequiemUser.PCE_INDEX_Name 	:{
						updateName();
						break;
					}
					case RequiemUser.PCE_INDEX_pass 	:{
						updatePass();
						break;
					}	
					case RequiemUser.PCE_INDEX_permissions	:{
						updatePermissions();
						break;
					}	
				default:
					break;
				}
			}
		}		
	}
	private class ListenerModifiedName implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			modifiedName();			
		}		
	}
	private class ListenerModifiedPass implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			modifiedNotizen();			
		}		
	}
	private class ListenerModifiedPermissions implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			modifiedPermissions();			
		}		
	}
	
}

